import store from '@/store'

const isNaN = value => value === null || value === undefined || value === ''

/**
 * 金额校验规则
 * @param label 标签--String
 * @param max 金额最大值--Number不能超过21474836.47
 * @param min 金额最小值--Number不能小于-21474836.48
 */
const money = (label, max, min) => {
    if (max === null || max === undefined || max > 21474836.47) max = 21474836.47
    if (min === null || min === undefined || min < 0) min = 0
    return (rule, value, callback) => {
        if (isNaN(value)) return callback(new Error(`${ label }不能为空`))
        if (!/^\d+(\.\d{1,2})?$/.test(value)) { return callback(new Error(`${ label }必须大于等于${ min }，且最多保留小数点后两位`)) }
        const amount = parseFloat(value)
        if (min !== null && min !== undefined && amount < min) { return callback(new Error(`${ label }不能小于${ min }`)) }
        if (max !== null && max !== undefined && amount > max) { return callback(new Error(`${ label }不能大于${ max }`)) }
        callback()
    }
}
/** ***************************************电话号码********************************************************/
const errorPhone = (phoneNumber) => !/^(0|86|17951)?(13[0-9]|14[5-9]|15[0-35-9]|16[6]|17[0-8]|18[0-9]|19[0-35-9])[0-9]{8}$/.test(phoneNumber)
/**
 * 手机号校验规则
 */
const phone = (required) => {
    if (required) {
        return (rule, value, callback) => {
            if (isNaN(value)) return callback(new Error('手机号不能为空'))
            if (errorPhone(value)) { return callback(new Error('请输入正确的手机号')) }
            callback()
        }
    } else {
        return (rule, value, callback) => {
            if (isNaN(value)) return callback()
            if (errorPhone(value)) { return callback(new Error('请输入正确的手机号')) }
            callback()
        }
    }
}
/**
 * 寄件人电话号码校验规则
 * @param expressCode 快递公司编码--String
 */
const SenderPhone = (expressCode) => {
    return (rule, value, callback) => {
        if (expressCode === 'shunfeng' || expressCode === 'shunfengkuaiyun') {
            if (isNaN(value)) callback(new Error('顺丰快递和顺丰速运必须填写寄件人电话号码！'))
            else if (errorPhone(value)) callback(new Error('请输入正确的手机号码！'))
            else callback()
        } else callback()
    }
}

/**
 * 邮箱校验规则
 */
const email = (required) => {
    if (required) {
        return (rule, value, callback) => {
            if (isNaN(value)) return callback(new Error('邮箱不能为空'))
            if (!/^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/.test(value)) { return callback(new Error('请输入正确的邮箱格式')) }
            callback()
        }
    } else {
        return (rule, value, callback) => {
            if (isNaN(value)) return callback()
            if (!/^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/.test(value)) { return callback(new Error('请输入正确的邮箱格式')) }
            callback()
        }
    }
}

/**
 * 使用校验规则
 * @param verify
 */
const useVerify = (verify) => {
    return [{validator: () => verify, trigger: 'blur'}]
}

/**
 * 使用自定义校验函数可传入form作为参数
 * @param func 自定义校验函数传递表单数据作为参数,要求返回新函数,新函数接收(rule,value,callback)
 * @param trigger 触发方式
 */
const useFunc = (func, trigger = 'blur') => {
    return [{validator: form => func(form), trigger}]
}

/**
 * 判断当前用户是否具有权限
 * @param permissions 需要的权限,满足其一即可
 * @returns boolean
 */
const hasPerm = (...permissions) => {
    const needPermit = permissions
    const userPerms = store.getters && store.getters.permissions
    const all_permission = '*:*:*'
    if (needPermit && needPermit instanceof Array && needPermit.length > 0)
        return userPerms.some(userPerm => all_permission === userPerm || needPermit.includes(userPerm))
    return true
}

export default {
    money,
    phone,
    SenderPhone,
    email,
    useVerify,
    useFunc,
    hasPerm
}
